package br.com.intelsys.basics.utils;

import java.util.Date;

public class DateOpers {

	public enum Field {
		YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
	}

	public static int getDifference(Date earlierDate, Date laterDate,
			Field field) {
		long dt = (laterDate.getTime() - earlierDate.getTime())
				+ getMilesecondByField(Field.HOUR);
		int diff = (int) (dt / getMilesecondByField(field)) + 1;
		return diff >= 0 ? diff : diff * -1;
	}

	private static long getMilesecondByField(Field field) {
		switch (field) {
		case SECOND:
		default:
			return 1 * 1000;
		case MINUTE:
			return 60 * 1000;
		case HOUR:
			return 60 * 60 * 1000;
		case DAY:
			return 60 * 60 * 24 * 1000;
		case MONTH:
			return 60 * 60 * 24 * 30 * 1000;
		case YEAR:
			return 60 * 60 * 24 * 30 * 365 * 1000;
		}
	}
}
